package ipl.projet.servlets;

import ipl.projet.domaine.Partie;
import ipl.projet.domaine.Partie.Etat;
import ipl.projet.exceptions.NomDePartieDuplique;
import ipl.projet.exceptions.PartieInconnueException;
import ipl.projet.usecase.UccStarTrek;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CreerPartie extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private UccStarTrek ucc;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sessionTEST = request.getSession();
		if (sessionTEST == null || sessionTEST.getAttribute("login") == null) {
			response.sendRedirect(response
					.encodeRedirectURL("connexion.html?erreur=1"));
			return;
		}
		
		if (request.getParameter("ajaxNomPartie") != null) {
			try {
				Partie partie = ucc.getPartie(request
						.getParameter("ajaxNomPartie"));
				if (partie.getEtat() == Etat.ANNULEE
						|| partie.getEtat() == Etat.EN_COURS
						|| partie.getEtat() == Etat.FINIE) {
					response.getWriter().write("KO");
					return;
				}
				if (partie.getEtat() == Etat.EN_COURS_PLACEMENT) {
					HttpSession session = request.getSession();
					session.setAttribute("partie", request.getParameter("ajaxNomPartie"));
					response.getWriter().write("OK");
					return;
				}
			} catch (PartieInconnueException e) {
				response.getWriter().write("KO");
				return;
			}
		} else {

			getServletContext().getNamedDispatcher("creerPartie.html").forward(
					request, response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("creer") != null) {
			HttpSession session = request.getSession();
			String login = (String) session.getAttribute("login");
			try {
				ucc.creerPartie(login, request.getParameter("nom"));
			} catch (NomDePartieDuplique e) {
				response.sendRedirect(response
						.encodeRedirectURL("creerPartie.html?erreur=1"));
				return;
			}

			request.setAttribute("nomPartie", request.getParameter("nom"));
			getServletContext().getNamedDispatcher("creerPartie.html").forward(
					request, response);
		} else if (request.getParameter("quitter") != null) {
			if (!request.getParameter("nom").isEmpty()) {
				try {
					ucc.annulerPartie(request.getParameter("nom"));
				} catch (PartieInconnueException e) {
					response.sendRedirect(response
							.encodeRedirectURL("creerPartie.html?erreur=2"));
					return;
				}
			}

			response.sendRedirect(response.encodeRedirectURL("accueil.html"));
			return;
		}
	}
}
